from django.shortcuts import render
from rest_framework.views import APIView
from .models import *
from rest_framework.response import Response
from .sers import *
# Create your views here.



class LoginAPIView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = User.objects.filter(username=username,password=password).first()
        id = user.id
        if user:
            return Response({'msg':'登陆成功','status':200,'id':id})
        else:
            return Response({'msg':'登陆失败','status':500,'id':id})


class GoodsAPIView(APIView):
    def get(self,request):
        queryset = Goods.objects.all()
        ser = GoodsSerializer(queryset,many=True)
        return Response(ser.data)

class OrderView(APIView):
    def post(self,request):
        ser = OrderSerializer(data=request.data)
        ser.is_valid(raise_exception=True)
        ser.save()
        # breakpoint()
        return Response({'msg':'添加成功'})

    def get(self,request):
        queryset = Order.objects.all()
        ser = OrderSerializer(queryset,many=True)
        return Response(ser.data)

